import Vue from 'vue'
const { $Toast, $Message } = require('../../static/iview/base/index');

// 分开本地或上线请求地址
let url = '';//接口公共地址
let bjurl = '';
if (process.env.NODE_ENV == 'development') {
    url = 'https://socard.busypi.com'//服务器地址
    // url = 'http://192.168.1.148:8090';//本地地址
    bjurl = 'https://socard.busypi.com/web_img/';
} else {
    url = 'https://socard.busypi.com'
    bjurl = 'https://socard.busypi.com/web_img/';
}
// 处理时间
function getDate() {
    let time = new Date();
    let timeYear = time.getFullYear() + "";
    let timeMonth = time.getMonth() + 1 + "";
    let timeDay = time.getDate() + "";
    timeMonth = timeMonth.length > 1 ? timeMonth : "0" + timeMonth;
    timeDay = timeDay.length > 1 ? timeDay : "0" + timeDay;
    return {
        year: timeYear,
        month: timeYear + "-" + timeMonth,
        day: timeYear + "-" + timeMonth + '-' + timeDay
    };
}
// 封装请求
/* 
    obj.loadingHied => 请求时不提示加载弹窗
    obj.that => 作用域this 不传则在回调中this指向错误 (箭头函数除外)
    obj.tpye => 请求方式 
    obj.url => 请求路径 .
    obj.params => 请求参数
    obj.success => 请求成功回调, 回调函数参数是返回值
    obj.fail => 请求成功回调, 回调函数参数是返回值
    
*/
/* 
    封装请求
    surl 请求地址 str
    type 请求方式 str
    params 请求参数 obj
    none 请求时提示 bool
*/
function ajax(surl, type, params, none) {
    let memberid = wx.getStorageSync('member_id');
    let membertype = wx.getStorageSync('member_type');
    let stafftype = wx.getStorageSync('staff_type');
    if (!none) {
        wx.showLoading({
            mask: true,
            title: '正在加载中....'
        });
    }
    // 返回一个Promise实例对象
    return new Promise((resolve, reject) => {
        wx.request({
            url: url + surl,
            data: params || {},
            timeout: 60000,
            header: { 'content-type': 'application/json; charset=utf-8', memberid, membertype, stafftype },
            method: type,
            success: res => {
                if (!none) {
                    wx.hideLoading();
                }
                if (res.statusCode === 200) {
                    resolve(res.data)
                } else {
                    console.log('error:', res.data);
                    reject(res.data)
                }
            },
            fail: err => {
                if (!none) {
                    wx.hideLoading();
                }
                console.log('error:', err);
                reject(err)
            },
            complete: function (e) {
            }
        });
    })
};
/* 
    如果未授权,重新授权
    scope 是否授权判断字段
    callback 成功或失败回调 true成功 false失败
*/
// 状态栏
function navColor(params = {}) {
    // params.title = params.title || '有π生活';
    // params.color = params.color || '#ffffff';
    // params.bjcolor = params.bjcolor || 'FF514E';
    wx.setNavigationBarTitle({
        title: params.title
    })
    // wx.setNavigationBarColor({
    //     frontColor: params.color,//前景颜色值，包括按钮、标题、状态栏的颜色，仅支持 #ffffff 和 #000000 (微信小程序官方规定)
    //     backgroundColor: 'FF514E'//背景颜色值，有效值为十六进制颜色
    // })
};
// 登录验证
function selectPhone(phone, path, param, type) {
    if (
        phone == "" ||
        phone == null ||
        phone == undefined
    ) {
        this.$toast({
            content: "请先登录~"
        });
    } else if (param != "") {
        wx.navigateTo({
            url: "../" + path + "/main?" + param + "=" + type
        });
    }
    else wx.navigateTo({
        url: "../" + path + "/main"
    });
};
//按钮防止多次点击
function btnDisabled(disabled) {
    disabled = true
    console.log("disabled1", disabled);
    setTimeout(() => {
        disabled = false
        console.log("disabled2", disabled);
    }, 20000);
};
// 中央总线 
let bus = new Vue({
    data() {
        return {
            shopCarCount: 0,//全局购物车数量
            shareQuery: null,//分享时所带参数
        }
    },
})
export default {
    time: getDate,
    ajax,
    url,
    navColor,
    bjurl,
    ajaxUrl: url,
    btnDisabled,
    bus,
    selectPhone
}